package Dao.Impl;

import Dao.AccountDao;
import Entity.account;
import Util.DBUtil;

import java.sql.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AccountDaoImpl implements AccountDao {
    @Override
    public List<account> AllGetAccount() throws SQLException {
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id, deal_type, pet_id, " +
                "                      seller_id, buyer_id, price, " +
                "                   deal_time FROM  account");

        List<account> accounts = new ArrayList<account>();
        account a = null;
        while(rs.next()){
            a=new account();
            a.setId(rs.getInt("id"));
            a.setDeal_type(rs.getString("deal_type"));
            a.setPet_id(rs.getInt("pet_id"));
            a.setSeller_id(rs.getInt("seller_id"));
            a.setBuyer_id(rs.getInt("buyer_id"));
            a.setPrice(rs.getInt("price"));
            a.setDeal_time(rs.getDate("deal_time"));
            accounts.add(a);
        }

        return accounts;
    }
    public List<account> GetAccountbystore(int id) throws SQLException {
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id, deal_type, pet_id, " +
                "                      seller_id, buyer_id, price, " +
                "                   deal_time FROM  account where buyer_id="+id);

        List<account> accounts = new ArrayList<account>();
        account a = null;
        while(rs.next()){
            a=new account();
            a.setId(rs.getInt("id"));
            a.setDeal_type(rs.getString("deal_type"));
            a.setPet_id(rs.getInt("pet_id"));
            a.setSeller_id(rs.getInt("seller_id"));
            a.setBuyer_id(rs.getInt("buyer_id"));
            a.setPrice(rs.getInt("price"));
            a.setDeal_time(rs.getDate("deal_time"));
            accounts.add(a);
        }

        return accounts;
    }
    @Override
    public account AllGetAccount(int id) throws SQLException {
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id, deal_type, pet_id, " +
                "                      seller_id, buyer_id, price, " +
                "                   deal_time FROM  account where id="+id);

            account a = null;
            rs.next();
            a=new account();
            a.setId(rs.getInt("id"));
            a.setDeal_type(rs.getString("deal_type"));
            a.setPet_id(rs.getInt("pet_id"));
            a.setSeller_id(rs.getInt("seller_id"));
            a.setBuyer_id(rs.getInt("buyer_id"));
            a.setPrice(rs.getInt("price"));
            a.setDeal_time(rs.getDate("deal_time"));


        return a;
    }

    @Override
    public void add(account a) throws SQLException, ParseException {

        Connection conn = DBUtil.getConnection();

        String sql = "insert into account (id, deal_type, pet_id, " +
                "      seller_id, buyer_id, price, " +
                "      deal_time)" +
                "    values (null,?,?,?,?,?,?)";

        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        ptmt.setString(1, a.getDeal_type());
        ptmt.setInt(2, a.getPet_id());
        ptmt.setInt(3, a.getSeller_id());
        ptmt.setInt(4, a.getBuyer_id());
        ptmt.setInt(5,a.getPrice());
        ptmt.setDate(6,  new java.sql.Date(a.getDeal_time().getTime()));

        //执行
        ptmt.execute();
    }

    @Override
    public void update(account a) throws SQLException {

    }

    @Override
    public boolean delete(account a) {
        return false;
    }
}
